package com.czy.controller;

import com.czy.pojo.Clazz;
import com.czy.pojo.ClazzPage;
import com.czy.pojo.PageResult;
import com.czy.pojo.Result;
import com.czy.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    /**
     * 分页查询班级信息
     */
    @GetMapping
    public Result ClazzList(ClazzPage clazzPage) {
        log.info("分页查询班级数据：{}", clazzPage);
        PageResult<Clazz> result = clazzService.page(clazzPage);
        return Result.success(result);
    }

    /**
     * 新增班级信息
     */
    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz) {
        log.info("新增班级信息：{}", clazz);
        clazzService.insert(clazz);
        return Result.success();
    }

    /**
     * 根据ID查询班级信息
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        log.info("根据ID查询班级信息：{}", id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    /**
     * 修改班级信息
     */
    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        log.info("修改班级信息：{}", clazz);
        clazzService.updateById(clazz);
        return Result.success();
    }

    /**
     * 根据ID删除班级信息
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除班级信息：{}", id);
        clazzService.deleteById(id);
        return Result.success();
    }

    /**
     * 查询所有班级信息
     */
    @GetMapping("/list")
    public Result list(){
       List<Clazz> clazzList = clazzService.findAll();
       return Result.success(clazzList);
    }
}
